﻿@using TextAdventures.Quest;
@using WebEditor.Views.Edit;
@model WebEditor.Models.Controls.EditorControl

@{
    IEditorControl ctl = Model.Control;
    bool isFirst = Model.IsFirst;
    string controlType = Model.ControlType;
    string caption = Model.Caption;

    object value;
    value = (ctl.Attribute == null) ? null : Model.EditorData.GetAttribute(ctl.Attribute);
    if (caption == null)
    {
        caption = ctl.Caption;
    }
    switch (controlType)
    {
        case "checkbox":
            @Html.CheckBox(ctl.Attribute, value as bool? == true, new { @class = "elementEditorCheckbox" })
            @Html.Label(ctl.Attribute, caption)
            break;
        case "textbox":
            @Html.TextBox(ctl.Attribute, (string)value, new { style = "width: 100%", @class = "elementEditorTextbox" })
            break;
        case "list":
            @Html.Action("EditStringList", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case @"script":
            @Html.Action("EditScript", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case "attributes":
            var data = (IEditorDataExtendedAttributeInfo) Model.EditorData;
            <div id="attributesListEditor">
                <div id="attributesListScroller" style="">
                    <table>
                        <thead>
                            <tr class="ui-corner-tr ui-widget-header">
                                <th class="attributeName">Name</th>
                                <th class="attributeValue">Value</th>
                                <th class="attributeSource">Source</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var attr in data.GetAttributeData())
                            {
                                <tr id="attr__@(attr.AttributeName)" class="attributeRow">
                                    <td class="attributeName">@Html.Raw(attr.AttributeName)</td>
                                    <td class="attributeValue">@Html.Raw(EditorUtility.GetDisplayString(data.GetAttribute(attr.AttributeName)))</td>
                                    <td class="attributeSource">@Html.Raw(attr.Source)</td>                  
                                </tr>
                            } 
                        </tbody>
                    </table>
                </div>
                <div id="attributeRowEditor" style="display: none;">
                </div>
            </div>
            break;
        case "label":
            <div class="elementEditorLabel">
                @{
                    var href = ctl.GetString("href");
                    if (href == null)
                    {
                        @caption
                    }
                    else
                    {
                        <a href="@href" target="_blank">@caption</a>
                    }
                }
            </div>
            break;
        case "title":
            string className = isFirst ? "elementEditorTitleTop" : "elementEditorTitle";
            <div class="@className">@caption</div>
            break;
        case "dropdown":
            string selectedItem = value as string;
            IEnumerable<SelectListItem> valuesList = ControlHelpers.GetDropdownValues(ctl, selectedItem, Model.Controller);
            @Html.DropDownList(ctl.Attribute, valuesList, new { @class = "elementEditorDropdown" })
            break;
        case "richtext":
            var richTextModel = new WebEditor.Models.Controls.RichTextControl
            {
                Control = ctl,
                // replace any <br/> tags with NewLine character
                Value = ((string)value).Replace("<br/>", Environment.NewLine).Replace("<br />", Environment.NewLine)
            };
            ControlHelpers.PopulateRichTextControlModel(ctl, Model.Controller, richTextModel);
            Html.RenderPartial("Controls/RichTextControl", richTextModel);
            break;
        case "number":
            string minMax = string.Empty;
            int? min = ctl.GetInt("minimum");
            int? max = ctl.GetInt("maximum");
            if (min.HasValue)
            {
                minMax += string.Format("min={0}", min);
            }
            if (max.HasValue)
            {
                if (minMax.Length > 0)
                {
                    minMax += " ";
                }
                minMax += string.Format("max={0}", max);
            }
            <input type="number" name="@ctl.Attribute" id="@ctl.Attribute" value="@Model.EditorData.GetAttribute(ctl.Attribute)" @minMax style="width: 50px" />
            break;
        case "numberdouble":
            string doubleMinMax = string.Empty;
            double? dblMin = ctl.GetDouble("minimum");
            double? dblMax = ctl.GetDouble("maximum");
            if (dblMin.HasValue)
            {
                doubleMinMax += string.Format("min={0}", dblMin);
            }
            if (dblMax.HasValue)
            {
                if (doubleMinMax.Length > 0)
                {
                    doubleMinMax += " ";
                }
                doubleMinMax += string.Format("max={0}", dblMax);
            }
            double? increment = ctl.GetDouble("increment");
            string step = string.Empty;
            if (increment.HasValue)
            {
                step = string.Format("step={0}", increment);
            }
            <input type="number" name="@ctl.Attribute" id="@ctl.Attribute" value="@Model.EditorData.GetAttribute(ctl.Attribute)" @doubleMinMax @step style="width: 50px" />
            break;
        case "multi":
            @RenderMultiControl(ctl, value)
            break;
        case "dropdowntypes":
            @Html.DropDownList("types-dropdown-" + ctl.Id, ControlHelpers.GetDropDownTypesControlItems(ctl, Model.Controller, Model.Key), new { @class = "types-dropdown", data_key = ctl.Id })
            break;
        case "elementslist":
            @Html.Action("ElementsList", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case "exits":
            @Html.Action("EditExits", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case "objects":
            IEditableObjectReference objectRef = value as IEditableObjectReference;
            string selectedValue = null;
            if (objectRef != null)
            {
                selectedValue = objectRef.Reference;
            }
            List<SelectListItem> items = new List<SelectListItem>(
                ControlHelpers.GetObjectListNames(ctl, Model.Controller).OrderBy(s => s, StringComparer.CurrentCultureIgnoreCase)
                              .Select(s => new SelectListItem { Text = s, Value = s, Selected = (selectedValue == s) })
                );
            // if ctl.Attribute is "key" (or any name that exists in the Model), ASP.NET MVC ignores Selected value
            @Html.DropDownList("dropdown-" + ctl.Attribute, items)
            break;
        case "verbs":
            @Html.Action("EditVerbs", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case "file":
            string source = string.Join(";", ctl.GetString("source").Split(';').Select(s => s.Substring(1)));
            <div style="display: inline-block; height: auto; width: auto;">
                <div style="float: left;">
                    @Html.TextBox(ctl.Attribute, (string) value, new { @readonly = "readonly", @class = "elementEditorFile" })
                </div>
                @if (ctl.GetBool("preview") && value is string && !string.IsNullOrEmpty((string) value))
                {
                    <div style="float: left">
                        <button type="button" class="img-preview" data-key="@ctl.Attribute" data-extensions="@source">
                            <img src="/ImageProcessor.ashx?h=80&w=80&gameId=@Model.GameId&image=@value.ToString()" />
                        </button>
                    </div>
                }
                <div style="float: left;">
                    <button type="button" class="file-upload" data-key="@ctl.Attribute" data-extensions="@source">Choose file</button>
                </div>
            </div>
            break;
        case "scriptdictionary":
            @Html.Action("EditScriptDictionary", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case "stringdictionary":
            @Html.Action("EditStringDictionary", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case "gamebookoptions":
            @Html.Action("EditGameBookOptions", new { id = Model.GameId, key = Model.Key, control = ctl })
            break;
        case "pattern":
            IEditableCommandPattern commandPattern = value as IEditableCommandPattern;
            string text = (commandPattern != null) ? commandPattern.Pattern : string.Empty;
            @Html.TextBox(ctl.Attribute, text, new { style = "width: 100%", @class = "elementEditorTextbox" })
            break;
        case null:
            break;
        default:
            throw new ArgumentException(string.Format("Invalid control type: {0}", controlType));
    }
}

@helper RenderMultiControl(IEditorControl ctl, object value)
{
    var model = new WebEditor.Models.Controls.MultiControl
    {
        GameId = Model.GameId,
        Key = Model.Key,
        Controller = Model.Controller,
        EditorData = Model.EditorData,
        Control = ctl,
        Value = value
    };
    Html.RenderPartial("Controls/MultiControl", model);
}